package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import users.Author;
import users.User;
import conference.Conference;
import conference.ConferenceIO;
import conference.TextParse;

/**
 * Root frame for displaying the GUI
 * @author Shawn Paul Smith - ssmith35
 */
public class TabbedConfFrame extends JFrame {
	// for serialization
	private static final long serialVersionUID =
			-7625670188018373625L;
	
	private final JTabbedPane myTabPanel = new JTabbedPane();

	private User myUser;
	private Conference myConf;
	private String myPath;
	private List<String> myAvailableConfs;
	
	public TabbedConfFrame(Conference conf, User user,
			String path, List<String> availableConfs) {
		myConf = conf;
		myUser = user;
		myPath = path;
		myAvailableConfs = availableConfs;
		try {
			UIManager.setLookAndFeel(UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {} //Oh well...
	}

	public void start() {
		setPreferredSize(new Dimension(1200, 700));
		setResizable(false);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		// setup header panel
		JPanel headerPanel = new JPanel();
		headerPanel.setBorder(BorderFactory
				.createRaisedBevelBorder());
		headerPanel.setLayout(new BorderLayout());
		JLabel appName = new JLabel("EZConference");
		appName.setFont(new Font("Sans Serif", Font.BOLD,
				24));
		headerPanel.add(appName, BorderLayout.WEST);
		add(headerPanel, BorderLayout.NORTH);
		
		//Add all tabs
		add(myTabPanel, BorderLayout.CENTER);
		myTabPanel.addTab("Choose Conference",
				new SwitchConfPanel());
		myTabPanel.addTab("Choose User",
				new SwitchUserPanel());
		addActionPanel();
		myTabPanel.setSelectedIndex(2);
		
		//When tabs change, remove all tabs to the right
		myTabPanel.addChangeListener(new ChangeListener() {			
			@Override
			public void stateChanged(ChangeEvent e) {
				for (int i = myTabPanel.getTabCount() - 1;
						i > myTabPanel.getSelectedIndex();
						i--) {
					myTabPanel.removeTabAt(i);
				}
			}
		});
		
		pack();
		setVisible(true);
	}
	
	/** Adds the correct panel to perform actions depending
	 * on user role. */
	private void addActionPanel() {
		JPanel newpanel = null;
		String role = myUser.getRole();
		if (role.equals("Author")) {
			newpanel = new SubmitPaperPanel(myConf, 
							(Author) myUser, myPath);
		}
		else if (role.equals("Reviewer")) {
			newpanel = new ReviewerPanel(myUser, myConf);
		}
		else if (role.equals("Program Chair")) {
			newpanel = new PCResultPanel(myConf);
		} else { //can't happen
			throw new IllegalStateException();
		}
		myTabPanel.addTab(role, newpanel);
	}
	
	// Panel to allow user to switch conferences
	private class SwitchConfPanel extends JPanel {
		private static final long serialVersionUID =
				243992626136902121L;

		public SwitchConfPanel() {
			final JComboBox box = new JComboBox(
					myAvailableConfs.toArray());
			box.setSelectedItem(myConf.getConfName());
			box.addActionListener(new ActionListener() {				
				@Override
				public void actionPerformed(ActionEvent e) {
					String confName = box.getSelectedItem()
							.toString();
					myConf = ConferenceIO.loadConference(
							myPath, confName );
					if (myConf == null) {
						//first time this conference was loaded
						if (!createConference(confName)) {
							System.err.println(
								"Unable to create conf");
						}
					}
					myTabPanel.addTab("Choose User", 
							new SwitchUserPanel());
					myTabPanel.setSelectedIndex(1);
				}
			});
			add(box);			
		}
	}
	
	private class SwitchUserPanel extends JPanel {
		private static final long serialVersionUID =
				243992626136902121L;

		public SwitchUserPanel() {
			final List<String> userList =
					new ArrayList<String>();
			for(User user : myConf.getUsers()) {
				if (user != null) {
				userList.add(user.getName() + " -- " +
						user.getRole());
				}
			}
			final JComboBox box = new JComboBox(
					userList.toArray());
			box.setSelectedItem(String.format("%s -- %s",
						myUser.getName(), myUser.getRole()));
			box.addActionListener(new ActionListener() {				
				@Override
				public void actionPerformed(ActionEvent e) {
					//switch to new user
					String[] userString = 
							box.getSelectedItem().toString()
							.split(" -- ");
					for(User user : myConf.getUsers()) {
						if (user.getName().equals(
									userString[0]) &&
								user.getRole().equals(
									userString[1])) {
							myUser = user;
							break;
						}
					}
					addActionPanel();
					myTabPanel.setSelectedIndex(2);
				}
			});
			add(box);
			
		}
	}
	
	private boolean createConference(String confName) {
		boolean created = false;
		try {
			File confFolder = new File(myPath);
			if (!confFolder.exists()) {
				confFolder.mkdir();
			}
			TextParse tp = new TextParse(new File("config.txt"));
			myConf = new Conference(tp.getChair(confName),
					confName, null, null,
					TextParse.getUsers(confName));			
			created = ConferenceIO.saveConference(
					myPath, myConf);
		} catch (IOException e) { e.printStackTrace(); }
		return created;
	}
}
